package com.xiaoj;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.locks.LockSupport;

/**
 * 测试 常用方法
 */
public class ThreadDemo3 {

    public static void main(String[] args) throws InterruptedException {
        TestMethod testMethod = new TestMethod();
        Thread thread = new Thread(testMethod);
        thread.setName("程序员小杰");
        thread.setPriority(2);
        thread.setDaemon(true);
        thread.start();
        for (int i = 0; i < 3; i++) {
            System.out.println("给程序员小杰加油~~~");
            Thread.sleep(2000);
        }

        boolean alive = thread.isAlive();
        System.out.println("是否存活：" + alive);
        boolean interrupted = thread.isInterrupted();
        System.out.println("判断此线程是否被中断: " + interrupted);
        //进行中断
        thread.interrupt();
        boolean interrupted2 = thread.isInterrupted();
        System.out.println("判断此线程是否被中断: " + interrupted2);
        boolean alive2 = thread.isAlive();
        System.out.println("是否存活：" + alive2);


    }
}

class TestMethod implements Runnable {

    Integer count = 0;
    @Override
    public void run() {
        Thread.yield();
        while(true){
            for (int i = 0; i < 5; i++) {
                System.out.println(Thread.currentThread().getName() + "正在跑步，已经跑了" + (++count) +"公里");
            }
            SimpleDateFormat format = new SimpleDateFormat("HH:MM:ss");
            try {
                System.out.println("跑累了，让我休息一会~~~~~ 当前时间：" +  format.format(new Date()));
                //休眠 15 秒
                Thread.sleep(15000);
                System.out.println("我休息好了~~~~ 当前时间：" +  format.format(new Date()));
            } catch (InterruptedException e) {
                //当前线程被 interrupt 后，就会发生 InterruptedException 异常
                System.out.println("休什么休，快起来跑~~~~ 当前时间：" +  format.format(new Date()));
            }
        }

    }
}